package com.education.common.model;

import com.alibaba.fastjson.util.TypeUtils;
import com.sun.org.apache.xpath.internal.operations.Bool;

import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.*;

/**
 * 封装map类型转换
 * @author 余一
 * @date 2021/9/14  15:00
 */
public class ModelBeanMap extends LinkedHashMap {

    public static ModelBeanMap create(){
        return new ModelBeanMap();
    }

    public static ModelBeanMap by(Object key,Object value){
        return create().put(key,value);
    }

    public ModelBeanMap put(Object key,Object value){
        super.put(key,value);
        return this;
    }

    public ModelBeanMap set(Object key,Object value){
        super.put(key,value);
        return this;
    }

    public String getStr(Object key){
        Object value = get(key);
        return TypeUtils.castToString(value);
    }

    public Integer getInt(Object key){
        Object value = get(key);
        return TypeUtils.castToInt(value);
    }

    public BigInteger getBigInteger(Object key){
        Object value = get(key);
        return TypeUtils.castToBigInteger(value);
    }

    public Long getLong(Object key){
        Object value = get(key);
        return TypeUtils.castToLong(value);
    }

    public Date getDate(Object key){
        Object value = get(key);
        return TypeUtils.castToDate(value);
    }

    public Time getTime(Object key){
        return (Time) get(key);
    }

    public Timestamp getTimestamp(Object key){
        return (Timestamp) get(key);
    }

    public Double getDouble(Object key){
        Object value = get(key);
        return TypeUtils.castToDouble(value);
    }

    public Float getFloat(Object key){
        Object value = get(key);
        return TypeUtils.castToFloat(value);
    }

    public Short getShort(Object key){
        Object value = get(key);
        return TypeUtils.castToShort(value);
    }

    public Boolean getBoolean(Object key){
        Object value = get(key);
        return TypeUtils.castToBoolean(value);
    }

    public java.math.BigDecimal getBigDecimal(Object key) {
        Object value = get(key);
        return TypeUtils.castToBigDecimal(value);
    }

    public byte[] getBytes(Object key) {
        Object value = get(key);
        return TypeUtils.castToBytes(value);
    }

    public <T> List<T> getList(Object key) {
        Object object = get(key);
        return object == null ? null : (List<T>) get(key);
    }

    public <T> Set<T> getSet(Object key) {
        return (Set<T>) get(key);
    }

    public List<ModelBeanMap> getModelBeanMapList(Object key){
        Object value = get(key);
        if (!(value instanceof List)) {
            throw new ClassCastException(value + "can not cast to list");
        }
        List list = (List)value;
        List<ModelBeanMap> result = new ArrayList<>();
        list.forEach(item -> {
            if (item instanceof Map) {
                ModelBeanMap modelBeanMap = new ModelBeanMap();
                Map data = (Map)item;
                data.keySet().forEach(mapKey -> {
                    modelBeanMap.put(mapKey, data.get(mapKey));
                });
                result.add(modelBeanMap);
            }
        });
        return result;
    }
}
